package com.zxshhy.controller;

import com.alibaba.fastjson.JSONObject;
import com.zxshhy.entity.Order;
import com.zxshhy.service.OrderService;
import com.zxshhy.vo.OrderVo;
import com.zxshhy.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zxs
 */
@RefreshScope
@RestController
@RequestMapping("/mq/order")
@Slf4j
public class OrderMQController {

    @Value("${zxshhy.seata.data-source-proxy-mode:XXXX}")
    private String trxMode;
    @Resource
    private OrderService orderService;

    /**
     * 采用消息队列实现分布式事务
     * @param orderVo
     * @return
     */
    @PostMapping("/createOrderByMQ")
    public ResultVo createOrderByMQ(@RequestBody OrderVo orderVo){
        log.info("使用MQ下单请求,用户:{}, 商品编号:{},trxMode={}", orderVo.getUserId(), orderVo.getCommodityCode(),trxMode);
        Order order = orderService.saveOrderByMQ(orderVo);
        log.info("\n下单结果：{}", JSONObject.toJSONString(order));
        return ResultVo.ok().put("order",order);
    }

    /**
     * 获取订单详情信息
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderDetail")
    public ResultVo getOrderDetail(@RequestParam String orderId){
        log.info("获取订单详情信息参数:{}", orderId);
        Order order = orderService.getOrderById(orderId);
        return ResultVo.ok().put("order",order);
    }

}
